﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Oracle.DataAccess.Client;

namespace CrmApplication.Models
{
    public class PositionModels
	{
		#region RegionMappingPosition
		
		public decimal Id{ get; set; }
        public string Name { get; set; }

		#endregion

		#region RegionOracleConnect

		private static OracleConnection connect;

        static PositionModels()
        {
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["HomeOrcl"].ConnectionString;
            connect = new OracleConnection(connString);
        }
		#endregion

		#region RegionPosition
		/// <summary>
        /// select positions list
        /// </summary>
        /// <param name="searchDomain"></param>
        /// <returns></returns>
        public static List<PositionModels> GetPosition(string searchPos)
        {
            string selectPos = "where position_name like '" + searchPos + "%'";
            string selectString = "SELECT id_position,position_name from position "
                                    + selectPos
                                    + " order by position_name ";

            OracleCommand loginComm = new OracleCommand(selectString);
            loginComm.Connection = connect;

            List<PositionModels> result = new List<PositionModels>();
            PositionModels result1 = null;
            try
            {
                connect.Open();
                using (OracleDataReader sdr = loginComm.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        result1 = new PositionModels();
                        result1.Id = sdr.GetDecimal(0);
                        result1.Name = sdr.GetString(1);                        
                        result.Add(result1);
                    }
                }
            }
            finally
            {
                connect.Close();
            }
            return result;
        }

		/// <summary>
		/// check if position name already exists into database
		/// </summary>
		/// <param name="position"></param>
		/// <returns></returns>
        public static PositionModels CheckPosition(string position)
        {
            string selectString = "select id_position,position_name from position where position_name=:positionName ";

            OracleCommand loginComm = new OracleCommand(selectString);
            loginComm.Connection = connect;

            loginComm.Parameters.Add(":positionName", position);

            PositionModels result = null;

            try
            {
                connect.Open();
                using (OracleDataReader sdr = loginComm.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        result = new PositionModels();
                        result.Id = sdr.GetDecimal(0);
                        result.Name= sdr.GetString(1);
                    }
                }
            }
            finally
            {
                connect.Close();
            }
            return result;
        }

        /// <summary>
        /// insert new position
        /// </summary>
        /// <param name="domainName"></param>
        public static void AddPosition(string positionName)
        {
            string insert = "INSERT INTO position(position_name) VALUES (:positionName)";

            OracleCommand usercomm = new OracleCommand(insert);

            usercomm.Parameters.Add(":positionName", positionName);

            usercomm.Connection = connect;
            try
            {
                connect.Open();
                usercomm.ExecuteNonQuery();
            }
            finally
            {
                connect.Close();
            }
        }

        /// <summary>
        /// creat positions array for json
        /// </summary>
        /// <param name="searchDomain"></param>
        /// <returns></returns>
        public static PositionContainer GetPosArray(string searchPos)
        {
            PositionModels[] data = new PositionModels[20];
            data = PositionModels.GetPosition(searchPos).ToArray();

            PositionContainer container = new PositionContainer()
            {
                Records = data,
                Count = data.Length
            };

            return container;
		}
		#endregion
	}

    public class PositionContainer
    {
        public PositionModels[] Records { get; set; }
        public int Count { get; set; }
    }
}